/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package facade;

import java.util.ArrayList;
import local.RecommendationFacadeLocal;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import model.Recommendation;
import model.Theme;

/**
 *
 * @author Randy
 */
@Stateless
public class RecommendationFacade implements RecommendationFacadeLocal {

    @PersistenceContext(unitName = "IEMPPU")
    private EntityManager em;

    @Override
    public ArrayList<Recommendation> getRecommendations(Long theme_id) {
        ArrayList<Recommendation> rList = new ArrayList<Recommendation>();
        Theme t = em.find(Theme.class, theme_id);

        for (Recommendation r : t.getRecommendation()) {
            rList.add(r);
        }

        return rList;
    }

    @Override
    public void updateRecommendation(Long recommend_id, String shortDesc, String longDesc, Double min, Double max) {
        Recommendation r = em.find(Recommendation.class, recommend_id);

        r.setShort_desc(shortDesc);
        r.setLong_desc(longDesc);
        r.setLower_range(min);
        r.setUpper_range(max);
        em.merge(r);
    }

    @Override
    public void createRecommendation(Long theme_id, String shortDesc, String longDesc, Double min, Double max) {
        Recommendation r = new Recommendation();
        r.setShort_desc(shortDesc);
        r.setLong_desc(longDesc);
        r.setLower_range(min);
        r.setUpper_range(max);

        em.persist(r);

        Theme t = em.find(Theme.class, theme_id);
        t.getRecommendation().add(r);
        em.merge(t);
    }

    @Override
    public void deleteRecommendation(Long theme_id, Long recommend_id) {
        Theme t = em.find(Theme.class, theme_id);
        Recommendation r = em.find(Recommendation.class, recommend_id);
        
        t.getRecommendation().remove(r);
        
        em.persist(t);
        em.remove(r);
    }

    @Override
    public Recommendation getRecommendation(Long recommend_id) {
        Recommendation r = em.find(Recommendation.class, recommend_id);
        
        return r;
    }
}
